package com.study.javaFx;

import com.study.javaFx.view.LabView;
import com.study.javaFx.view.MachineView;
import com.study.javaFx.view.MachineTypeView;
import com.study.javaFx.view.StaffView;
import com.study.javaFx.view.UsageView;
import com.study.javaFx.view.UserView;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.effect.InnerShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Accordion;
import javafx.scene.control.TextArea;

public class MainView {
    public void show(Stage primaryStage) {
        String[] modules = {"机房", "机器", "机型", "员工", "上机记录", "用户"};
        String[] actions = {"查询", "新增", "修改", "删除"};

        // --- 主内容布局 ---
        VBox mainContentLayout = new VBox(20);
        mainContentLayout.setAlignment(Pos.TOP_CENTER);
        mainContentLayout.setPadding(new Insets(20));
        mainContentLayout.setStyle("-fx-background-color: linear-gradient(from 0% 0% to 0% 100%, #4FA3D5, #3C8ABE);");
        
        // 创建标题区域
        HBox titleContainer = new HBox(20);
        titleContainer.setAlignment(Pos.CENTER);

        // 加载图片
        ImageView takagiImageView = new ImageView(new Image("image/高木同学.jpg"));
        takagiImageView.setFitHeight(120);
        takagiImageView.setPreserveRatio(true);

        // 创建标题文本
        Text mainTitle = new Text("欢迎来到高木同学的机房！");
        mainTitle.setStyle("-fx-font-size: 32px; -fx-font-weight: bold; -fx-fill: #FFFFFF;");

        titleContainer.getChildren().addAll(takagiImageView, mainTitle);
        mainContentLayout.getChildren().add(titleContainer);

        VBox titledPanesContainer = new VBox(10);
        titledPanesContainer.setStyle("-fx-background-color: #FFE8C9;");

        for (String module : modules) {
            VBox box = new VBox(20);
            box.setPadding(new Insets(40, 60, 40, 60));
            box.setAlignment(Pos.CENTER);
            box.setStyle("-fx-background-color: #FFEFE1; -fx-border-color: #E0E0E0; -fx-border-width: 0 1 1 1;");

            Text titleText = new Text(module + " 操作");
            titleText.setStyle("-fx-font-size: 18px; -fx-font-weight: bold; -fx-text-alignment: center; -fx-fill: #333;");
            VBox titleBox = new VBox(titleText);
            titleBox.setAlignment(Pos.CENTER);
            titleBox.setPrefHeight(40);
            box.getChildren().add(titleBox);

            GridPane buttonGrid = new GridPane();
            buttonGrid.setAlignment(Pos.CENTER);
            buttonGrid.setHgap(20);
            buttonGrid.setVgap(15);

            LabView labView = new LabView();
            MachineView machineView = new MachineView();
            MachineTypeView machineTypeView = new MachineTypeView();
            StaffView staffView = new StaffView();
            UsageView usageView = new UsageView();
            UserView userView = new UserView();

            for (int i = 0; i < actions.length; i++) {
                String action = actions[i];
                Button btn = new Button(action);
                btn.setPrefWidth(180);
                btn.setPrefHeight(38);
                btn.setStyle("-fx-font-size: 15px; -fx-background-color: linear-gradient(from 0% 100% to 100% 0%, #F3F342, #FEFEE9); -fx-text-fill: #333; -fx-border-color: #BFBFBF; -fx-border-radius: 5; -fx-background-radius: 5;");

                // 添加凹陷效果
                InnerShadow innerShadow = new InnerShadow(3, Color.rgb(0, 0, 0, 0.4));
                innerShadow.setOffsetX(2);
                innerShadow.setOffsetY(2);

                btn.setOnMousePressed(event -> btn.setEffect(innerShadow));
                btn.setOnMouseReleased(event -> btn.setEffect(null));

                if (module.equals("机房")) {
                    switch (action) {
                        case "查询":
                            btn.setOnAction(e -> labView.showQueryWindow());
                            break;
                        case "新增":
                            btn.setOnAction(e -> labView.showAddWindow());
                            break;
                        case "修改":
                            btn.setOnAction(e -> labView.showUpdateWindow());
                            break;
                        case "删除":
                            btn.setOnAction(e -> labView.showDeleteWindow());
                            break;
                    }
                } else if (module.equals("机器")) {
                    switch (action) {
                        case "查询":
                            btn.setOnAction(e -> machineView.showQueryDialog());
                            break;
                        case "新增":
                            btn.setOnAction(e -> machineView.showAddWindow());
                            break;
                        case "修改":
                            btn.setOnAction(e -> machineView.showUpdateWindow());
                            break;
                        case "删除":
                            btn.setOnAction(e -> machineView.showDeleteWindow());
                            break;
                    }
                } else if (module.equals("机型")) {
                    switch (action) {
                        case "查询":
                            btn.setOnAction(e -> machineTypeView.showQueryWindow());
                            break;
                        case "新增":
                            btn.setOnAction(e -> machineTypeView.showAddWindow());
                            break;
                        case "修改":
                            btn.setOnAction(e -> machineTypeView.showUpdateWindow());
                            break;
                        case "删除":
                            btn.setOnAction(e -> machineTypeView.showDeleteWindow());
                            break;
                    }
                } else if (module.equals("员工")) {
                    switch (action) {
                        case "查询":
                            btn.setOnAction(e -> staffView.showQueryWindow());
                            break;
                        case "新增":
                            btn.setOnAction(e -> staffView.showAddWindow());
                            break;
                        case "修改":
                            btn.setOnAction(e -> staffView.showUpdateWindow());
                            break;
                        case "删除":
                            btn.setOnAction(e -> staffView.showDeleteWindow());
                            break;
                    }
                } else if (module.equals("上机记录")) {
                    switch (action) {
                        case "查询":
                            btn.setOnAction(e -> usageView.showQueryWindow());
                            break;
                        case "新增":
                            btn.setText("上机");
                            btn.setOnAction(e -> usageView.showAddWindow());
                            break;
                        case "修改":
                            btn.setText("下机");
                            btn.setOnAction(e -> usageView.showUpdateWindow());
                            break;
                        case "删除":
                            btn.setOnAction(e -> usageView.showDeleteWindow());
                            break;
                    }
                } else if (module.equals("用户")) {
                    switch (action) {
                        case "查询":
                            btn.setOnAction(e -> userView.showQueryWindow());
                            break;
                        case "新增":
                            btn.setOnAction(e -> userView.showAddWindow());
                            break;
                        case "修改":
                            btn.setOnAction(e -> userView.showUpdateWindow());
                            break;
                        case "删除":
                            btn.setOnAction(e -> userView.showDeleteWindow());
                            break;
                    }
                } else {
                    btn.setOnAction(e -> {
                        Alert alert = new Alert(Alert.AlertType.INFORMATION);
                        alert.setTitle("操作提示");
                        alert.setHeaderText(null);
                        alert.setContentText("你点击了 " + module + " 的 " + action + " 按钮");
                        alert.showAndWait();
                    });
                }
                buttonGrid.add(btn, i % 2, i / 2);
            }
            box.getChildren().add(buttonGrid);
            TitledPane pane = new TitledPane(module, box);
            pane.setExpanded(false);
            pane.setStyle("-fx-font-size: 18px; -fx-alignment: center; -fx-padding: 18 0 18 0;");
            titledPanesContainer.getChildren().add(pane);
        }
        
        ScrollPane scrollPane = new ScrollPane(titledPanesContainer);
        scrollPane.setFitToWidth(true);
        scrollPane.setStyle("-fx-background-color: transparent; -fx-border-color: transparent;");

        // 加快滚动速度
        final double speedFactor = 5.0;
        scrollPane.setOnScroll(event -> {
            if (event.getDeltaY() != 0) {
                double contentHeight = scrollPane.getContent().getBoundsInLocal().getHeight();
                double newVvalue = scrollPane.getVvalue() - (event.getDeltaY() * speedFactor / contentHeight);

                // 确保vvalue在[0, 1]范围内
                if (newVvalue < 0) {
                    newVvalue = 0;
                } else if (newVvalue > 1) {
                    newVvalue = 1;
                }
                
                scrollPane.setVvalue(newVvalue);
                event.consume(); // 消费事件以防止默认滚动
            }
        });

        mainContentLayout.getChildren().add(scrollPane);

        // --- 左下角按钮 ---
        VBox cornerContainer = new VBox(5);
        cornerContainer.setAlignment(Pos.BOTTOM_LEFT);
        cornerContainer.setPadding(new Insets(15));
        
        // 只保留ER图按钮
        Button erButton = new Button("查看ER 图");
        erButton.setStyle("-fx-font-size: 12px; -fx-background-color: #7f8c8d; -fx-text-fill: white; -fx-background-radius: 5;");
        // 添加凹陷效果
        javafx.scene.effect.InnerShadow innerShadow = new javafx.scene.effect.InnerShadow(3, javafx.scene.paint.Color.rgb(0, 0, 0, 0.4));
        innerShadow.setOffsetX(2);
        innerShadow.setOffsetY(2);
        erButton.setOnMousePressed(event -> erButton.setEffect(innerShadow));
        erButton.setOnMouseReleased(event -> erButton.setEffect(null));
        erButton.setOnAction(event -> {
            try {
                Stage erStage = new Stage();
                erStage.setTitle("数据库ER图");
                Image erImage = new Image("image/ER图.png");
                if (erImage.isError()) {
                    throw new Exception("图片加载失败，路径: image/ER图.png");
                }
                ImageView erImageView = new ImageView(erImage);
                erImageView.setPreserveRatio(true);

                StackPane imageWrapper = new StackPane(erImageView);
                imageWrapper.setStyle("-fx-background-color: #f0f0f0;");
                
                // 初始时让图片适应窗口大小
                erImageView.fitWidthProperty().bind(imageWrapper.widthProperty().subtract(20));
                erImageView.fitHeightProperty().bind(imageWrapper.heightProperty().subtract(20));

                Scene erScene = new Scene(imageWrapper, 1024, 768); // 直接使用StackPane作为场景根节点
                erStage.setScene(erScene);
                erStage.show();

            } catch (Exception e) {
                new Alert(Alert.AlertType.ERROR, "无法加载ER图，请确认文件存在于 src/main/resources/image/ER图.png").showAndWait();
            }
        });
        
        cornerContainer.getChildren().add(erButton);
        cornerContainer.setPickOnBounds(false);

        // 使用StackPane来放置主内容和左下角按钮
        StackPane rootStackPane = new StackPane(mainContentLayout, cornerContainer);
        StackPane.setAlignment(cornerContainer, Pos.BOTTOM_LEFT);

        Scene scene = new Scene(rootStackPane, 640, 600);
        primaryStage.setTitle("机房管理系统");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}